package org.hyh.transactional;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.aspectj.apache.bcel.util.ClassPath;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;

/**
 * @auther :hyh
 * @desc :
 * @date :2019/12/17
 */
@Configuration
@EnableTransactionManagement
@EnableAspectJAutoProxy(exposeProxy = true)
@ComponentScan(basePackages = "org.hyh.transactional")
@PropertySource("classpath:db.properties")
public class TransactionalConfig {
   @Autowired
	Environment environment;

   @Bean
   public DataSource   dataSource() throws PropertyVetoException {
	   ComboPooledDataSource dataSource = new ComboPooledDataSource();
	    dataSource.setUser(environment.getProperty("user"));
	    dataSource.setPassword(environment.getProperty("password"));
	    dataSource.setDriverClass(environment.getProperty("className"));
	    dataSource.setJdbcUrl(environment.getProperty("url"));
	    return  dataSource;
   }

   @Bean
	public PlatformTransactionManager platformTransactionManager(@Autowired DataSource dataSource) {
		DataSourceTransactionManager dst = new DataSourceTransactionManager(dataSource);
		return dst;
	}





	@Bean
	public JdbcTemplate jdbcTemplate(@Autowired DataSource dataSource) {
	   return new JdbcTemplate(dataSource);
   }

}
